﻿package com.designoutput.ui.controls
{
	import com.designoutput.enum.ToolType;

	import flash.display.*;
	import flash.events.*;

	public class ToolButton extends Sprite
	{
		public var type:String;
		private var _bg:Sprite;
		private var _icon:Bitmap;
		private var _marked:Boolean=false;

		public function ToolButton(type:String, icon:BitmapData, iconX:int, iconY:int, defaultOuterWidth:Number=26, defaultInnerWidth:Number=24, showBg:Boolean=true)
		{
			this.type = type;
			
			_bg = new Sprite();
			_bg.graphics.beginFill(7961731);
			_bg.graphics.drawRect(0, 0, defaultOuterWidth, defaultOuterWidth);
			_bg.graphics.beginFill(14606822);
			_bg.graphics.drawRect(1, 1, defaultInnerWidth, defaultInnerWidth);
			_bg.graphics.endFill();
			_bg.alpha=0;
			_bg.visible=showBg;
			addChild(_bg);
			
			_icon = new Bitmap(icon);
			_icon.alpha=0.7;
			_icon.x=iconX;
			_icon.y=iconY;
			addChild(_icon);
			
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		}

		private function mouseOverHandler(event:MouseEvent):void
		{
			_bg.alpha = 1;
			_icon.alpha = 1;
		}

		private function mouseOutHandler(event:MouseEvent):void
		{
			if (!_marked)
			{
				_bg.alpha = 0;
				_icon.alpha = 0.7;
			}
		}

		public function Mark(marked:Boolean):void
		{
			if(_marked != marked){
				_marked = marked;
				if (marked)
				{
					mouseOverHandler(null);
					_icon.x=_icon.x + 1;
					_icon.y=_icon.y + 1;
				}
				else
				{
					mouseOutHandler(null);
					_icon.x=_icon.x - 1;
					_icon.y=_icon.y - 1;
				}
			}
		}

		public function isMarked():Boolean
		{
			return _marked;
		}
	}
}
